package com.tanhua.dubbo.api.Impl;

import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.model.mogo.RecommendUser;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public RecommendUser TodayBest(Long userId) {
        Criteria criteria = new Criteria();
        criteria.where("toUserId").is(userId);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    @Override
    public PageResult recommendation(Integer page, Integer pagesize, Long to_user_id) {
        Criteria criteria = new Criteria();
        criteria.where("toUserId").is(to_user_id);
        Query query = Query.query(criteria);
        //总条数
        long count = mongoTemplate.count(query, RecommendUser.class);
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        //查询到的数据
        query.query(criteria).limit((page -1) * pagesize).skip(pagesize);
        PageResult pr = new PageResult(page,pagesize, (int) count,list);
        return pr;
    }
}
